<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">面经后台</el-breadcrumb-item>
      <el-breadcrumb-item>面经管理</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>共{{ total }}条记录</span>
        <el-button
          style="float: right"
          round
          icon="el-icon-plus"
          type="primary"
          size="small"
          @click="add"
          >添加面经</el-button
        >
      </div>
      <!-- <div v-for="o in 4" :key="o" class="text item">
        {{ '列表内容 ' + o }}
      </div> -->
      <el-table :data="articles" stripe>
        <el-table-column
          type="index"
          label="序号"
          width="100px"
        ></el-table-column>
        <el-table-column label="标题" prop="stem"></el-table-column>
        <el-table-column label="作者" prop="creator"></el-table-column>
        <el-table-column label="点赞" prop="likeCount"></el-table-column>
        <el-table-column label="浏览数" prop="views"></el-table-column>
        <el-table-column label="更新时间" prop="createdAt"></el-table-column>
        <el-table-column label="操作" align="center">
          <!-- <template #default="scope">
            {{ scope.$index }}--{{ scope.row }}
          </template> -->
          <template #default="scope">
            <el-button
              type="text"
              icon="el-icon-view"
              @click="preview(scope.row.id)"
            ></el-button>
            <el-button type="text"
              icon="el-icon-edit"
              @click="showEdit(scope.row.id)"
            ></el-button>
            <el-button
              type="text"
              icon="el-icon-delete"
              @click="del(scope.row.id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <el-pagination
        background
        layout="prev,pager,next"
        :total="total"
        :page-size="params.pageSize"
        :current-page="params.current"
        @current-change="changeCurrent"
      ></el-pagination>
    </el-card>
    <el-drawer
      :title="title"
      size="50%"
      :visible.sync="drawer"
      direction="rtl"
      @close="closeFn"
    >
      <el-form
        v-if="title !== '预览面经'"
        label-width="70px"
        :model="form"
        :rules="rules"
        ref="formRef"
      >
        <el-form-item label="标题" prop="stem">
          <el-input v-model="form.stem"></el-input>
        </el-form-item>
        <el-form-item label="内容" prop="content">
          <quill-editor
            v-model="form.content"
            @blur="$refs.formRef.validateField('content')"
          ></quill-editor>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="saveAdd">确认</el-button>
          <el-button>取消</el-button>
        </el-form-item>
      </el-form>
      <div v-else>
        <h3>{{ form.stem }}</h3>
        <el-divider></el-divider>
        <div v-html="form.content"></div>
      </div>
    </el-drawer>
  </div>
</template>

<script>
// require styles
import {
  getArticleList,
  delArticle,
  addArticle,
  getArticleDetail,
  updateArticle
} from '@/api/article'
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
import { quillEditor } from 'vue-quill-editor'
export default {
  name: 'article-page',
  created() {
    this.getArticleList()
  },
  data() {
    return {
      params: {
        current: 1,
        pageSize: 10
      },
      articles: [],
      total: null,
      tableData: [],
      drawer: false,
      title: '',
      form: {
        stem: '',
        content: ''
      },
      rules: {
        stem: [{ required: true, message: '不能为空', trigger: 'blur' }],
        content: [{ required: true, message: '不能为空', trigger: 'blur' }]
      }
    }
  },

  methods: {
    async getArticleList() {
      const res = await getArticleList(this.params)
      this.articles = res.data.rows
      this.total = res.data.total
    },
    changeCurrent(val) {
      console.log(val)
      this.params.current = val
      this.getArticleList()
    },
    async del(id) {
      try {
        await this.$confirm('此操作将永久删除, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        await delArticle(id)
        this.$message.success('删除成功')
        // 删除成功了  页面原有的数据就变旧了  重新拿一次最新的数据
        this.getArticleList()
        // catch (err)捕获错误
      } catch (err) {
        console.log(err)
      }
    },
    // 添加
    add() {
      // 解决编辑操作的第一步
      this.form = {
        stem: '', // 标题
        content: '' // 内容
      }
      this.drawer = true
      this.title = '添加面经'
    },
    closeFn() {
      if (this.title !== '预览面经') {
        this.$refs.formRef.resetFields()
      }
    },
    async saveAdd() {
      try {
        await this.$refs.formRef.validate()
        if (this.title === '修改面经') {
          const form = {}
          form.id = this.form.id
          form.stem = this.form.stem
          form.content = this.form.content
          await updateArticle(form)
          this.$message.success('修改成功')
        } else {
          await addArticle(this.form)
          this.$message.success('添加成功')
        }
        this.getArticleList()
        this.drawer = false
      } catch (err) {
        console.log(err)
      }
    },
    async showEdit(id) {
      this.title = '修改面经'
      const res = await getArticleDetail(id)
      console.log(res)
      this.form = res.data
      this.drawer = true
    },
    async preview(id) {
      this.title = '预览面经'
      const res = await getArticleDetail(id)
      console.log(res)
      this.form.stem = res.data.stem
      this.form.content = res.data.content
      this.drawer = true
    }
  },
  components: {
    quillEditor
  }
}
</script>

<style scoped lang="scss">
.box-card {
  margin-top: 15px;
  box-shadow: 0 1px 22px 0 rgb(0 0 0 / 10%);
}
.el-pagination {
  text-align: center;
  margin-top: 16px;
}
::v-deep .ql-editor {
  min-height: 200px;
}

::v-deep .el-drawer__body {
  padding: 20px;
}
</style>
